package cn.edu.jsu.nrs.dao.impl;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import cn.edu.jsu.nrs.dao.NewsDao;
import cn.edu.jsu.nrs.model.News;
import cn.edu.jsu.nrs.model.User;

/**
 * 
 * @author:YYH
 * @date:2013-1-24 下午08:17:12
 * @update-date:2013-1-24 下午08:17:12
 * @version:1.0
 * @project_name:NewsReleaseSystem
 * @description:新闻数据访问接口实现
 */
@Component(value = "newsDao")
public class NewsDaoImpl implements NewsDao {
	private HibernateTemplate hibernateTemplate;

	/**
	 * 保存新闻
	 */
	public int save(News news) {
		return (Integer) hibernateTemplate.save(news);
	}

	/**
	 * 更新新闻
	 */
	public void update(News news) {
		// hibernateTemplate.update(news);
		hibernateTemplate.update(news);
		// news=queryNewsById(1);
		// System.out.println("count:"+news.getClickCount());
	}

	/**
	 * 删除新闻
	 */
	public void delete(News news) {
		hibernateTemplate.delete(news);
	}

	/**
	 * 根据id删除新闻
	 */
	public int deleteNewsById(int id) {
		return hibernateTemplate.bulkUpdate("delete from News where news_id=?",
				id);
	}

	/**
	 * 批量删除新闻
	 */
	public void deleteNewsList(List<News> newslist) {
		hibernateTemplate.deleteAll(newslist);

	}

	// 根据id查询新闻
	public News queryNewsById(int id) {
		return (News) hibernateTemplate.get(News.class, id);
		// return (News) hibernateTemplate.get(News.class, id);
	}

	// 根据新闻类型Id查询所有新闻
	@SuppressWarnings("unchecked")
	public List<News> queryNewsByTypeId(int typeId) {
		return hibernateTemplate.find("from News where type_id=?", typeId);
	}


	// 查询10条热点新闻
	@SuppressWarnings("unchecked")
	public List<News> queryHotNews(final int first, final int count) {
		List<News> newslist = hibernateTemplate
				.executeFind(new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						/*  Criteria criteria =session.createCriteria(News.class);
						 criteria.addOrder(Order.desc("clickCount"));
						 criteria.setFirstResult(first);
						 criteria.setMaxResults(count);
						 return criteria.list();*/
						 
						Query query = session
								.createQuery("select new News(n.newsId,n.title) from News n order by n.clickCount desc");
						query.setFirstResult(first);
						query.setMaxResults(count);
						return query.list();
					}
				});
		return newslist;
	}

	// 查询10条最新新闻
	@SuppressWarnings("unchecked")
	public List<News> queryTopNews(final int first, final int count) {
		List<News> newslist = hibernateTemplate
				.executeFind(new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						/*
						 * Criteria criteria =
						 * session.createCriteria(News.class);
						 * criteria.addOrder(Order.desc("clickCount"));
						 */
						Query query = session
								.createQuery("select new News(n.newsId,n.title) from News n order by n.publishTime desc");
						query.setFirstResult(first);
						query.setMaxResults(count);
						return query.list();
					}
				});
		return newslist;

	}
	
	/**
	 * 根据类型Id查找新闻列表
	 * @param typeId
	 * @param count
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<News> queryNewsListByTypeIdAndCount(final int typeId,final int count){
		List<News> newslist = hibernateTemplate
				.executeFind(new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						Query query = session
								.createQuery("select new News(n.newsId,n.title) from News n where type_id=? order by n.publishTime desc");
						query.setParameter(0,typeId);
						query.setFirstResult(0);
						query.setMaxResults(count);
						return query.list();
					}
				});
		return newslist;
	}

	// 查询新闻的总记录数
	public int queryNewsCounts() {
		int result = 0;
		result = hibernateTemplate.find("from News").size();
		return result;
	}

	// 根据新闻类别查询新闻的总记录数
	public int queryNewsCounts(final int typeId) {
		int result = 0;

		result = hibernateTemplate.find("from News where type_id=?", typeId)
				.size();
		return result;

	}

	/**
	 * 新闻分页
	 */
	@SuppressWarnings("unchecked")
	public List<News> paging(final int beginIndex, final int size,
			final String propertyName) {
		List<News> newsList = hibernateTemplate
				.executeFind(new HibernateCallback() {

					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						Criteria criteria = session.createCriteria(News.class);
						if ((propertyName != null)
								&& (propertyName.length() > 0)) {
							criteria.addOrder(org.hibernate.criterion.Order
									.desc(propertyName));
						}
						criteria.setFirstResult((beginIndex - 1) * size);
						criteria.setMaxResults(size);
						return criteria.list();
					}
				});
		return newsList;
	}

	/**
	 * 根据类别查询分页新闻 新闻分页
	 */
	@SuppressWarnings("unchecked")
	public List<News> pagingByTypeId(final int typeId, final int beginIndex,
			final int size) {
		List<News> newslist = hibernateTemplate
				.executeFind(new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						Query query = session.createQuery("from News where type_id=? order by publishTime desc");
						//select new News(n.newsId,n.title) from News n where type_id=? order by n.publishTime desc
						query.setParameter(0, typeId);
						query.setFirstResult(beginIndex);
						query.setMaxResults(size);
						return query.list();
					}
				});
		return newslist;
	}

	// 添加点击数
	public void addClickCount() {
		hibernateTemplate.bulkUpdate("update News set clickCount=clickCount+1");
	}

/***
 * 根据关键字条件模糊查询新闻	
 * @param beginIndex
 * @param count
 * @param keyword
 * @param sord
 * @param sidx
 * @return
 */
@SuppressWarnings("unchecked")
public List<News> queryNewsListBySearch(final int beginIndex,final int count,final String keyword,final String sord,final String sidx){
		
		List<News> news = hibernateTemplate.executeFind(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql="select new News(n.newsId,n.title,n.content,n.keyWord,n.clickCount,n.modiferTime,n.publishTime)from News n";
						if (keyword!=null) {
							hql=hql+" where keyWord like '%"+keyword+"%'";
						}
						if (sidx==null) {
							hql=hql+" order by newsId desc";
						}else {
							hql=hql+" order by "+sidx+" "+sord;
						}
						Query query = session.createQuery(hql);
						query.setFirstResult(beginIndex);
						query.setMaxResults(count);
						return query.list();
					}
				});
		
		return news;
	}

	
	/**
	 * 根据关键字条件模糊查询新闻记录条数
	 */
	public int queryNewsCounts(final String keyword){
		String hql="from News";
		if (keyword!=null) {
			hql=hql+" where keyword like '%"+keyword+"%'";
		}
		return  hibernateTemplate.find(hql).size();
	}
	
	public List<News> queryHotNewsByTypeId(int typeId) {
		return null;
	}

	public List<News> queryTopNewsByTypeId(int typeId) {
		return null;
	}

	public long pageCountByTypeId(int typeId, int size) {
		return 0;
	}

	@Resource
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}
}
